package copyengine.ui.component.scrollablePanel.scrollPanel
{
    import copyengine.ui.component.CEGUIComponent;
    import copyengine.ui.component.scrollablePanel.common.layout.ICEScrollableLayout;
    import copyengine.ui.component.scrollablePanel.common.moveOperator.CEScrollableMoveOperator;
    import copyengine.ui.component.scrollablePanel.element.ICEScrollableElement;
    import copyengine.utils.GeneralUtils;

    import flash.geom.Rectangle;

    import starling.display.Canvas;
    import starling.display.DisplayObjectContainer;

    public class CEScrollPanel extends CEGUIComponent
    {
        protected var mMoveOperator:CEScrollableMoveOperator;
        protected var mScrollPanelCore:CEScrollPanelCore;
        protected var mLayout:ICEScrollableLayout;
        protected var mDirection:int;

        protected var mViewportRect:Rectangle;
        protected var mMaxMoveRect:Rectangle;


        public function CEScrollPanel(_container:DisplayObjectContainer,
                                      _layout:ICEScrollableLayout,
                                      _direction:int,
                                      _isLockInView:Boolean,
                                      _viewportWidth:int, _viewportHeight:int)
        {
            mLayout = _layout;
            mDirection = _direction;
            mLayout.direction = mDirection;

            super(_container);

            var mask:Canvas = new Canvas();
            mask.drawRectangle(0, 0, _viewportWidth, _viewportHeight);
            container.mask = mask;

            mViewportRect = new flash.geom.Rectangle(0, 0, _viewportWidth, _viewportHeight);
            mMaxMoveRect = new flash.geom.Rectangle(0, 0, _viewportWidth, _viewportHeight);

            mScrollPanelCore = new CEScrollPanelCore();
            mScrollPanelCore.viewportRe = mViewportRect;
            mScrollPanelCore.scrollPanelContainer = _container;

            mMoveOperator = new CEScrollableMoveOperator();
            mMoveOperator.initialize(this, _isLockInView);
        }

        override protected function doDispose():void
        {
            super.doDispose();
            mMoveOperator.dispose();
            mScrollPanelCore.dispose();
            mLayout.dispose();
            mMoveOperator = null;
            mScrollPanelCore = null;
            mLayout = null;
        }

        public function get direction():int {return mDirection;}

        public function get viewportRect():Rectangle {return mViewportRect;}

        public function get maxMoveRect():Rectangle {return mMaxMoveRect;}

        public function moveViewPort(_newPosX:Number, _newPosY:Number):void
        {
            if (mViewportRect.x != _newPosX || mViewportRect.y != _newPosY) {
                mViewportRect.x = _newPosX;
                mViewportRect.y = _newPosY;
                refresh();
            }
        }

        public function moveToBottom():void
        {
            mViewportRect.x = maxMoveRect.width;
            mViewportRect.y = maxMoveRect.height;
        }

        public function moveToTop():void
        {
            mViewportRect.x = 0;
            mViewportRect.y = 0;
        }

        /**
         * 无论添加还是删除Element 其函数只是对Element本身进行数组删除
         *
         * 不负责
         *
         * 1`  Element的Init/Dispose
         * 2`  ScrollPanel的 Refresh , 上层逻辑需要手动调用该函数进行刷新
         */
        public function addElement(_element:ICEScrollableElement):void
        {
            _element.scrollPanel = this;
            mScrollPanelCore.allSubElementVector.push(_element);
        }

        /**
         * 无论添加还是删除Element 其函数只是对Element本身进行数组删除
         *
         * 不负责
         *
         * 1`  Element的Init/Dispose
         * 2`  ScrollPanel的 Refresh , 上层逻辑需要手动调用该函数进行刷新
         */
        public function removeElement(_element:ICEScrollableElement):void
        {GeneralUtils.removeElementFromArrayOrVector(_element, mScrollPanelCore.allSubElementVector);}

        public function get allElementVector():Vector.<ICEScrollableElement> {return mScrollPanelCore.allSubElementVector;}

        public function set allElementVector(_v:Vector.<ICEScrollableElement>):void {mScrollPanelCore.allSubElementVector = _v;}

        public function refresh():void {mScrollPanelCore.refresh();}

        public function layoutAllElement():void { mLayout.layoutAllElement(mScrollPanelCore.allSubElementVector);}

        public function calculateMaxMoveRect():void
        {
            var maxWidth:int;
            var maxHeight:int;
            for each(var element:ICEScrollableElement in mScrollPanelCore.allSubElementVector) {
                maxWidth = Math.max(element.getBounds().x + element.getBounds().width);
                maxHeight = Math.max(element.getBounds().y + element.getBounds().height);
            }
            maxWidth = Math.max(0, maxWidth - mViewportRect.width);
            maxHeight = Math.max(0, maxHeight - mViewportRect.height);

            maxMoveRect.x = maxMoveRect.y = 0;
            maxMoveRect.width = maxWidth;
            maxMoveRect.height = maxHeight;

            mViewportRect.x = GeneralUtils.normalizingValue(mViewportRect.x, 0, maxWidth);
            mViewportRect.y = GeneralUtils.normalizingValue(mViewportRect.y, 0, maxHeight);
        }

    }
}
